package model.interactionhandler;

import java.util.Random;

import main.Log;
import model.entity.Avatar;
import model.entity.BasicEntity;
import model.item.TakeableItem;
import model.skill.SkillType;

public class PickPocketHandler extends InteractionHandler{
	
	public PickPocketHandler(String theName) {
		super(theName);
	}
	
	public boolean pickPocket(Avatar av, BasicEntity ent){
		if( youCanDoIt(av, ent) ){
			av.addItem( takeRandomItem(ent) );
			return true;
		}
		return false;
	}
	
	/**
	 * this is where we choose a random item from the inventory
	 * @param ent
	 * @return
	 */
	private TakeableItem takeRandomItem(BasicEntity ent){
		int inventorySize = ent.getInventory().getItems().size();
		Random randGen = new Random();
		int spot = randGen.nextInt(inventorySize);
		// get the Item in the Inventory at the random index
		return ent.removeItem(ent.getInventory().getItems().get(spot));	
	}
	
	/**
	 * this determines if the pickpocketing is possible thus it checks;
	 *   if you have enough ability (with some randomness applied)
	 *   if the NPC your are pickpocketing has an item
	 * @param av
	 * @param ent
	 * @return
	 */
	private boolean youCanDoIt(Avatar av, BasicEntity ent){
		// NOTE: pickpocketskill is between 0 and 100 always
		Random randGen = new Random();
		Log.out(Log.MODEL, "size of inventory: " + ent.getInventory().getItems().size());
		int inventorySize = ent.getInventory().getItems().size();
		// they didnt want PickPocket in boht skiltype and statype 
		// bc both would return the same string 
		if( (av.getStatVal(SkillType.PICKPOCKET) > randGen.nextInt(100)) 
				&& inventorySize > 0){
			return true;
		}
		return false;
	}
}